<?php
if (!defined('APPPATH')) exit('No direct script access allowed');

include_once(APPPATH.'libraries/api/core/MySQL.php');
include_once(APPPATH.'libraries/api/core/Services.php');

class Member extends Services {
    
	public static function memberGet($member_id) {
		if(parent::checkArguments($member_id)) {
			$query = "SELECT `member_id`, `firstname`, `lastname`, `username`, `email`  FROM `member` 
					  WHERE `member_id` = $member_id AND `deleted` != 1; ";
			$result = MySQL::selectGetRow($query);
			if(parent::check($result)) {
				return $result;
			} else {
				return array();
			}
		} else {
			return array();
		}
	}
        
	public static function memberAuthenticate($username, $password) {
		if(parent::checkArguments(array($username, $password))) {
			$query = "SELECT `member_id`, `firstname` FROM `member` 
					  WHERE `username` = '$username' AND `password` = MD5('$password') AND `deleted` != 1; ";
			$result = MySQL::selectGetRow($query);
			if(parent::check($result)) {
				return $result;
			} else {
				return array();
			}
		} else {
			return array();
		}
	}
    
	public static function memberAdminAuthenticate($username, $password) {
		if(parent::checkArguments(array($username, $password))) {
			$query = "SELECT `member_id`, `firstname` FROM `member` 
					  WHERE '$username' = 'superadmin' AND `password` = MD5('$password') AND `deleted` != 1; ";
			$result = MySQL::selectGetRow($query);
			if(parent::check($result)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}    

	public static function memberSet($firstname, $lastname, $username, $email, $password) {
		if(parent::checkArguments(array($firstname, $lastname, $username, $email, $password))) {
			$query = "INSERT INTO `member` (`firstname`, `lastname`, `username`, `email`, `password`, `deleted`) 
					  VALUES ('$firstname', '$lastname', '$username', '$email', md5('$password'), 0); ";
			$result = MySQL::update($query);
			if(parent::check($result)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	public static function memberUpdate($member_id, $firstname, $lastname, $username, $email, $password) {
		if(parent::checkArguments(array($member_id, $firstname, $lastname, $username, $email, $password))) {
			$query = "UPDATE `member` SET `firstname` = '$firstname', 
										  `lastname` = '$lastname', 
										  `username` = '$username', 
										  `email` = '$email', 
										  `password` = md5('$password')
					 WHERE `member_id` = $member_id;";
			$result = MySQL::update($query);
			if(parent::check($result)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	public static function memberDelete($member_id) {
		if(parent::checkArguments($member_id)) {
			$query = "UPDATE `member` SET `deleted` = 1 WHERE `member_id` = $member_id;";
			$result = MySQL::update($query);
			if(parent::check($result)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	public static function membersList() {
		$query = "SELECT `member_id`, `firstname`, `lastname`, `username`, `email` FROM `member` WHERE `deleted` != 1; ";
		$result = MySQL::selectGetArray($query);
		if(parent::check($result)) {
			return $result;
		} else {
			return array();
		}
	}

}

?>
